<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="hardcode2013.*" %>
<%@ page import="java.util.List" %>

<html>
	<head>
		<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
	</head>

	<body>
	
	<%@include file="include_header.jsp" %>

	<%
	final String currentUrl = "/mail.jsp";
	
	if (profile != null) {
		String pageStr = request.getParameter("page");
		int currentPage = 1;
		if (pageStr != null) {
			try {
				currentPage = Integer.parseInt(pageStr);
			} catch (Exception e) {
			}
		}
		
		if (currentPage <= 0) {response.sendRedirect(currentUrl); return;}
		List<ConversationProfile> conversationList = MailServ.getUserConversations(profile.getUsername());
		int totalConversations = conversationList.size();
		int totalPages = totalConversations / MailServ.resultsPerPage + (totalConversations % MailServ.resultsPerPage == 0 ? 0 : 1);
		if (currentPage > totalPages && totalConversations > 0) {response.sendRedirect(currentUrl + "?page=" + totalPages); return;}
		int totalMessages = 0;
		for (ConversationProfile c : conversationList) totalMessages += c.getMessagesCount();

		int startIndex = (currentPage - 1) * MailServ.resultsPerPage;
		int endIndex = startIndex + MailServ.resultsPerPage;
		if (endIndex > totalConversations) endIndex = totalConversations;
		conversationList = conversationList.subList(startIndex, endIndex);

		String pageLbl = "Total Conversations: " + totalConversations;
		if (totalConversations > 0)
			pageLbl += ", " +
				"Showing: " + ((currentPage - 1) * MailServ.resultsPerPage + 1) + "-" + (currentPage * MailServ.resultsPerPage > totalConversations ? totalConversations : currentPage * MailServ.resultsPerPage) + ", " +
				"Page: " + currentPage + " / " + totalPages +
				(currentPage > 1 ? " <a href=\"" + EncodeUtil.encodeHtml(currentUrl) + "?page=" + (currentPage - 1) + "\">Prev</a>" : "") +
				(currentPage < totalPages ? " <a href=\"" + EncodeUtil.encodeHtml(currentUrl) + "?page=" + (currentPage + 1) + "\">Next</a>" : "");
			pageLbl += "<br>" + "Total Messages: " + totalMessages;
		%>
	
		<fieldset>
			<legend>Conversation Info</legend>
			A conversation is created per partner per item.<br>
			A conversation can contain multiple messages.
		</fieldset>
		
		<fieldset>
			<legend>Mailbox</legend>
			
			<a href="/send_mail.jsp">Send Mail</a><br>
			
			<%= pageLbl %>
			
		    <table border="1" width="100%">
			    <tr>
				    <td>Item</td>
					<td>Original Price</td>
			  	    <td>Partner</td>
			  	    <td>Starter</td>
			   	    <td>Creation Date</td>
			   	    <td>Last Message Date</td>
			   	    <td>Messages</td>
			   	    <td>Last Reply User</td>
			   	    <td>Read</td>
			   	    <td>Action</td>
			    </tr>
				<%
				for (ConversationProfile c : conversationList) {
					%>
				    <tr>
				    	<td><%=EncodeUtil.encodeHtml((c.getItemKeyStr() != null ? ItemServ.getItemProfile(c.getItemKeyStr()).getTitle() : "-"))%></td>
						<td><%=EncodeUtil.encodeHtml((c.getItemPrice() != null ? c.getItemPrice() : "-")) %></td>
					    <td><%=EncodeUtil.encodeHtml((c.getStarter().equals(profile.getUsername()) ? c.getResponder() : c.getStarter()))%></td>
					    <td><%=EncodeUtil.encodeHtml(c.getStarter())%></td>
				  	    <td><%=EncodeUtil.encodeHtml(c.getCreationDateTime())%></td>
				  	    <td><%=EncodeUtil.encodeHtml(c.getLastMessageDateTime())%></td>
				  	    <td><%=EncodeUtil.encodeHtml(String.valueOf(c.getMessagesCount()))%></td>
				  	    <td><%=EncodeUtil.encodeHtml(c.getLastReplyUser())%></td>
				  	    <td><%=EncodeUtil.encodeHtml((c.getLastReplyUser().equals(profile.getUsername()) ? "-" : String.valueOf(c.getIsRead()).replace("true", "Yes").replace("false", "No"))) %></td>
				  	    <td>
				  	    <form action="/mail" method="get">
					  	    <% if (c.getItemKeyStr() != null) { %> <input type="hidden" name="item_key" value="<%= EncodeUtil.encodeHtml(c.getItemKeyStr()) %>" /> <% } %>
					  	    <input type="hidden" name="partner" value="<%= EncodeUtil.encodeHtml((c.getStarter().equals(profile.getUsername()) ? c.getResponder() : c.getStarter())) %>" />
					  	    <input type="hidden" name="action" value="view_conversation" />
					  	    <input type="hidden" name="auth_key" value="<%= SessionUtil.getAuthKey(request) %>">
					  	    <input type="submit" value="View" />
				  	    </form>
				  	    </td>
				    </tr>
					<%
				}
				%>
			</table>
		</fieldset>
		<%
	}
	%>
	</body>
</html>